package com.iotproject.studyservice.controller;

import com.iotproject.commonutils.R;
import com.iotproject.studyservice.entity.UserRememberWord;
import com.iotproject.studyservice.service.StudyWordService;
import com.iotproject.studyservice.service.UserRememberWordService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/studyservice/forget-word")
public class ForgetWordController {

    @Autowired
    StudyWordService studyWordService;

    @Autowired
    UserRememberWordService userRememberWordService;

    //第一次记住，第二次忘记，数据库修改remember为0，继续下个单词学习
    @GetMapping("forgetWord/{wordId}")
    public R forgetWord(@PathVariable("wordId") String wordId, HttpServletRequest httpServletRequest) {
        //由于点击记住的时候可能会向数据库(看 user_remember_word 表，user_study_word 不用管)增加记录，如果增加了记录，那么就把它删除
        String memberUserId = studyWordService.getMemberUserId(httpServletRequest);
        QueryWrapper<UserRememberWord> userRememberWordQueryWrapper = new QueryWrapper<>();
        userRememberWordQueryWrapper.eq("user_id", memberUserId);
        userRememberWordQueryWrapper.eq("remember_word_id", wordId);
        int rememberWordRecord = userRememberWordService.count(userRememberWordQueryWrapper);
        if (rememberWordRecord != 0) {
            boolean remove = userRememberWordService.remove(userRememberWordQueryWrapper);
        }

        return R.ok();
    }


}
